package com.cdd.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdd.role.entity.RolePermission;
import com.cdd.role.mapper.RolePermissionMapper;
import com.cdd.role.service.RolePermissionService;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-03  15:58:02
 */
@Service
public class RolePermissionServiceImpl implements RolePermissionService {
    //根据角色id分配权限  -先删除角色具有的权限--在分配
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public CommonResult updateRolePermission(String roleId, List<String> menuids) {
        //根据角色id删除
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("role_id",roleId);
        int delete = rolePermissionMapper.delete(wrapper);

            //添加权限
            for (String menuid:menuids) {
                RolePermission rolePermission=new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(menuid);
                rolePermission.setIsDeleted(0);
                rolePermissionMapper.insert(rolePermission);

        }
        return new CommonResult(2000, "权限分配成功", null);
    }
}
